package ttk.muxiuesd.entity;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Disposable;
import ttk.muxiuesd.util.Position;

/**
 * 实体
 */
public abstract class Entity implements Disposable {

    private static int idIndex = 0;

    public final int id;
    public Group group;
    public EntityGroup entityGroup;
    public float maxHealth; // 生命值上限
    public float curHealth; // 当前生命值
    public float speed, curSpeed;

    public float x, y;
    public float width, height;
    public float originX, originY;
    public float scaleX = 1, scaleY = 1;
    public float rotation;
    public TextureRegion textureRegion;
    public Rectangle hurtbox = new Rectangle();

    public boolean attacked = false;

    public Entity(EntityGroup entityGroup, Group group, float maxHealth, float curHealth) {
        this.id = idIndex++;
        this.entityGroup = entityGroup;
        this.group = group;
        this.maxHealth = maxHealth;
        this.curHealth = curHealth;
        //此处已经把实体加入实体组
        this.entityGroup.add(this);
    }

    public void draw(Batch batch) {
        if (this.attacked) {
            //受到攻击变红
            batch.setColor(255, 0, 0, 255);
        }
        if (textureRegion != null) {
            batch.draw(textureRegion, x, y,
                originX, originY,
                width, height,
                scaleX, scaleY, rotation);
        }
        //还原batch
        batch.setColor(255, 255, 255, 255);
        attacked = false;
    }

    public void update(float delta) {
        //生命值小于等于0后移除实体并且直接返回
        if (this.curHealth <= 0f) {
            this.entityGroup.remove(this);
            return;
        }
        setCullingArea(x, y, width, height);
    }

    public void setCullingArea(float x, float y, float width, float height) {
        this.hurtbox.set(x, y, width, height);
    }

    public void dispose() {
        if (this.textureRegion != null) {
            this.textureRegion = null;
        }
    }

    public void setOrigin(float originX, float originY) {
        this.originX = originX;
        this.originY = originY;
    }

    /**
     * Sets the position of the actor's bottom left corner.
     */
    public void setPosition(float x, float y) {
        if (this.x != x || this.y != y) {
            this.x = x;
            this.y = y;
        }
    }

    /**
     * Sets the width and height.
     */
    public void setSize(float width, float height) {
        if (this.width != width || this.height != height) {
            this.width = width;
            this.height = height;
        }
    }

    /**
     * Set bounds the x, y, width, and height.
     */
    public void setBounds(float x, float y, float width, float height) {
        setPosition(x, y);
        setSize(width, height);
    }

    public Position getPosition () {
        return new Position(this.x, this.y);
    }
    public void setPosition (Position position) {
        this.x = position.x;
        this.y = position.y;
    }

    public float getWidth () {
        return this.width;
    }
    public float getHeight () {
        return this.height;
    }
}
